<?php
function get_system_info()
{
	$system_info = array();

	/* 检查系统基本参数 */
	$system_info[] = array("操作系统", PHP_OS);
	$system_info[] = array("php版本", PHP_VERSION);

	/* 检查MYSQL支持情况 */
	$mysql_enabled = function_exists('mysql_connect') ? "支持" : "<b class=\"red\">不支持</b>";
	$system_info[] = array("Mysql 数据库支持", $mysql_enabled);
	
	/* 检查Sqlite支持情况*/
	//$sqlite_enabled = function_exists('sqlite_open') ? "支持" : "<b class=\"red\">不支持</b>";
	//$system_info[] = array("Sqlite 数据库支持", $sqlite_enabled);
	
	/* 检查Rewrite支持情况*/
	//if ( function_exists('apache_get_modules') && in_array('mod_rewrite',apache_get_modules()) ){
//		$rewrite_enabled =  '支持';
//	}else{
//		$rewrite_enabled =  '<b class=\"red\">不支持</b>';
//	}
//	$system_info[] = array("Rewrite 支持", $rewrite_enabled);
	/* 检查图片处理函数库 */
	$gd_ver = get_gd_version();
	$gd_ver = empty($gd_ver) ? "<b class=\"red\">不支持</b>" : $gd_ver;
	if ($gd_ver > 0)
	{
		if (PHP_VERSION >= '4.3' && function_exists('gd_info'))
		{
			$gd_info = gd_info();
			$jpeg_enabled = ($gd_info['JPG Support']        === true) ? "支持" : "不支持";
			$gif_enabled  = ($gd_info['GIF Create Support'] === true) ? "支持" : "不支持";
			$png_enabled  = ($gd_info['PNG Support']        === true) ? "支持" : "不支持";
		}
		else
		{
			if (function_exists('imagetypes'))
			{
				$jpeg_enabled = ((imagetypes() & IMG_JPG) > 0) ? "支持" : "不支持";
				$gif_enabled  = ((imagetypes() & IMG_GIF) > 0) ? "支持" : "不支持";
				$png_enabled  = ((imagetypes() & IMG_PNG) > 0) ? "支持" : "不支持";
			}
			else
			{
				$jpeg_enabled = "不支持";
				$gif_enabled  = "不支持";
				$png_enabled  = "不支持";
			}
		}
	}
	else
	{
		$jpeg_enabled = "不支持";
		$gif_enabled  = "不支持";
		$png_enabled  = "不支持";
	}
	$system_info[] = array("GD库", $gd_ver);
	$system_info[] = array("JPG组件支持", $jpeg_enabled);
	$system_info[] = array("GIF组件支持",  $gif_enabled);
	$system_info[] = array("PNG组件支持",  $png_enabled);
	
	/* 检查EXIF支持情况*/
//	$exif_enabled = function_exists('exif_read_data') ? "支持" : "<b class=\"red\">不支持</b>";
//	$system_info[] = array("EXIF 支持", $exif_enabled);
	
	/* 服务器是否安全模式开启 */
	$safe_mode = ini_get('safe_mode') == '1' ? "打开" : "关闭";
	$system_info[] = array("安全模式", $safe_mode);
	
	return $system_info;
}
function get_gd_version()
{
	static $version = -1;

	if ($version >= 0)
	{
		return $version;
	}

	if (!extension_loaded('gd'))
	{
		$version = 0;
	}
	else
	{
		// 尝试使用gd_info函数
		if (PHP_VERSION >= '4.3')
		{
			if (function_exists('gd_info'))
			{
				$ver_info = gd_info();
				preg_match('/\d/', $ver_info['GD Version'], $match);
				$version = $match[0];
			}
			else
			{
				if (function_exists('imagecreatetruecolor'))
				{
					$version = 2;
				}
				elseif (function_exists('imagecreate'))
				{
					$version = 1;
				}
			}
		}
		else
		{
			if (preg_match('/phpinfo/', ini_get('disable_functions')))
			{
				/* 如果phpinfo被禁用，无法确定gd版本 */
				$version = 1;
			}
			else
			{
			  // 使用phpinfo函数
			   ob_start();
			   phpinfo(8);
			   $info = ob_get_contents();
			   ob_end_clean();
			   $info = stristr($info, 'gd version');
			   preg_match('/\d/', $info, $match);
			   $version = $match[0];
			}
		 }
	}
	return $version;
}

function get_url()
{
	$dir = $_SERVER['SCRIPT_NAME'];
	$dir = substr(dirname($dir), 0, -7);
	$url = 'http://' . $_SERVER['SERVER_NAME'] . $dir;
	return $url;
}

function creat_mysql($setting)
{
	global $install_error, $dbconn;
	$dbconn = @mysql_connect($setting['dbhost'].':'.$setting['dbport'],$setting['dbuser'],$setting['dbpass']);
	if(!$dbconn)
	{
		$install_error = "无法连接数据库！请确认数据库参数是否正确！Error:".mysql_error();
		return;
	}
    return true;
	$create_db = mysql_query('CREATE DATABASE `' . $setting['dbname'] . '` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;',$dbconn);
	
	if(!$create_db)
	{
		$install_error = "创建数据库失败！Error:".mysql_error();
		return;
	}
	$select_db = @mysql_select_db($setting['dbname'],$dbconn);
	$install_error .= '数据库创建成功。。。<br />';
	return true;
}
function create_conf($setting)
{
	global $install_error;
	$eval = "<?php
define('HOST', '" . $setting['dbhost'] . "');
define('USER', '" . $setting['dbuser'] . "');
define('PASS', '" . $setting['dbpass'] . "');
define('DB_NAME', '" . $setting['dbname'] . "');
define('PREFIX', '" . $setting['dbpre'] . "');
?>";
	$install_error .= '配置文件安装成功。。。<br />';
	if(!@file_put_contents(ROOTDIR . 'conf/database.inc.php', $eval))
	{
		$install_error = '<font color="red">无法创建配置文件。。。</font><br />';
		return;
	}
	@chmod(ROOTDIR . 'conf/database.inc.php', 0755);
	@file_put_contents(ROOTDIR . 'conf/install.lock', date('Y-m-d H:i:s') . ' Installed!');
	@chmod(ROOTDIR.'conf/install.lock',0755);
	return true;
}
function create_table($setting)
{
	global $dbconn, $install_error;
	$pre      = $setting['dbpre'];
	$username = $setting['username'];
	$password = sha1($setting['userpass']);
	include ROOTDIR . 'install/data.php';
	mysql_query('set names "utf8"', $dbconn);
	foreach($sql as $kk => $ss)
	{
		$ss = array_filter(explode(';', $ss));
		foreach($ss as $sss)
		{
			mysql_query($sss, $dbconn);
		}
		$install_error .= "数据表 $kk 创建成功。。。<br />";
	}
	return true;
}
?>